<style>
.score {text-align:right;}
.failed {color: red}
.focus {background:#ff0}
.sent {margin:10px; padding:3px; background: #ff5; border: #ccc solid thin}
.printScore{margin:10px; padding:3px; background:#5ff; border: #ccc solid thin; cursor: pointer}
#stage-list table{margin:5px; padding:2px;border:#aaa solid thin;background:#ccc}
#stage-list table th {background:#cdd}
#stage-list table td {padding:2px 10px}
</style>
<script type="text/javascript">
function checkFail()
{
	$.each($(".score"),function(){
		var s = $(this).text();
	    if (s !='' && s < 60)
	    	  $(this).addClass('failed');
	 });
}


$(document).ready(function(){
    $("#sn").change(function(){
        $("#stageForm").submit();
    });
    $("#stage_id").change(function(){
    	   $("#stageForm").submit();
     });

    $("#signBtn").click(function(){
        $("#editMode").attr('value',1);
        $(this).attr('disabled','disabled');
        $("#stageForm").submit();
     });

    $("#saveBtn").click(function(){
    	$("#stageForm").attr('action','<?php echo $this->route('save')?>');
   	    $(this).attr('disabled','disabled');
        $("#stageForm").submit();
     });

    $("#cancelBtn").click(function(){
        $("#stageForm").attr('action','<?php echo $this->route('index')?>');
        $("#stageForm").submit();
     });

    $("#exportBtn").click(function(){
        if (confirm('<?php echo $this->translate("After the export performance can not be modified, such as still have questions, please unlock the Registry staff")?>')) {
        	 $("#stageForm").attr('action','<?php echo $this->route('export')?>');
        	 $(this).attr('disabled','disabled');
             $("#stageForm").submit();
        }
     });

    $("#printScore").click(function(){
    	 $("#stageForm").attr('action',"<?php echo $this->route('print-score')?>");
    	 $("#stageForm").attr('target',"_blank");
         $("#stageForm").submit();
         $("#stageForm").attr('action',"<?php echo $this->route('index')?>");
         $("#stageForm").attr('target',"");
     });

    $("input:text").keydown(function(e){
        var tabIndex =  parseInt($(this).attr('tabindex'));
        switch (e.which) {
        case 9 : //tab
        	var score = parseFloat($(this).attr('value'));
            if (isNaN(score))  score = -1;
            if (score < 0 || score > 100) {
                $(this).attr('value','');
                tabIndex --;
            }

            break;
        case 13 : // return
            var score = parseFloat($(this).attr('value'));
            if (isNaN(score)) score = -1;
            if (score < 0 || score > 100) {
                alert($(this).attr('value')+' <?php echo $this->translate('value error')?>');
                $(this).attr('value','');
            }
            else
                tabIndex++;
            break;
        case 40 :
        	tabIndex ++;
         break;
        case 38 :
        	tabIndex --;
         break;
        }
        if ($("input[tabindex="+tabIndex+"]").length) {
            $("input[tabindex="+tabIndex+"]").focus();
        }
    });

    $("input:text").focus(function(){
        $("input:text").removeClass('focus');
        $(this).addClass('focus')
       });

    $("#stage-list table tbody tr:even").addClass('even');
    $("#stage-list table tbody tr:odd").addClass('odd');
    checkFail();
});
</script>
<div id="stage-data">
<form id="stageForm"  method="post" action="<?php echo $this->route('index')?>">
<input type="hidden" id="editMode" name="editMode" value="0" />
<div id="stage-menu">
<select id="sn" name="sn">
<?php foreach($this->teacherCourse as $val):?>
<option value="<?php echo $val['sn']?>" <?php if ($this->sn == $val['sn']):?>selected<?php endif?>><?php echo $val['className']?> - <?php echo $val['name']?></option>
<?php endforeach?>
</select>

<select id="stage_id" name="stage_id">
<?php if($this->currStage == 255):?>
<option value="<?php echo $this->currStage?>"><?php echo $this->translate('whole stage score')?></option>
<?php else:?>
<?php for($i=1; $i <= $this->period; $i++):?>
<option value="<?php echo $i?>" <?php if($i==$this->currStage):?>selected<?php endif?> ><?php echo sprintf($this->translate('%dth stage'),$i)?></option>
<?php endfor;?>
<?php endif?>
</select>
<?php if (!$this->sendmit):?>
<?php if ($this->editMode):?>
<input type="button" id="saveBtn" value="<?php echo $this->translate('save')?>" />
<input type="button" id="cancelBtn" value="<?php echo $this->translate('cancel')?>" />
<?php else:?>
<input type="button" id="signBtn" value="<?php echo $this->translate('sign')?>" />
<input type="button" id="exportBtn"
 <?php if (!$this->scores or (!$this->scoreAverage and $this->currStage!=255)):?>disabled="disabled"<?php endif?>
  value="<?php echo $this->translate('Export stage score')?>" />
<?php endif?>

<?php else:?>
<span class="printScore" id="printScore"><?php echo $this->translate('Friendly Print')?></span>
<span class="sent"><?php echo $this->translate('Score have been sent the Registry staff')?></span>
<?php endif?>
</div>
<div id="stage-list">
<table>
<thead>
<tr>
<th><?php echo $this->translate('number')?></th>
<th><?php echo $this->translate('student name')?></th>
<?php if($this->currStage == 255):?>
<th><?php echo $this->translate('whole stage score')?></th>
<?php else:?>
<th><?php echo $this->translate('Regular Assessment')?> * <?php echo $this->ratioArr[$this->currStage][1]?>%</th>
<th><?php echo $this->translate('normal score')?> * <?php echo $this->ratioArr[$this->currStage][2]?>%</th>
<th><?php echo $this->translate('Average')?></th>
<?php endif;?>
</tr>
</thead>
<tbody>
<?php $i=0;?>
<?php foreach($this->students as $sn=>$val):?>
<tr>
<td><?php echo $val['number']?></td>
<td><?php echo $val['name']?></td>
<?php if($this->currStage == 255):?>
<?php if ($this->editMode):?>
<td class="score"><input type="text"  tabindex="<?php echo 100+$i++?>"  size="3" name="score[3][<?php echo $val['sn']?>]"
value="<?php if (isset($this->scores[$sn][3]))echo $this->scores[$sn][3]?>" /></td>
<?php else:?>
<td class="score"><?php if(isset($this->scores[$sn][3])) echo $this->scores[$sn][3]?></td>
<?php endif?>
<?php else:?>
<?php if ($this->editMode):?>
<td class="score"><input type="text"  tabindex="<?php echo 100+$i?>"  size="3" name="score[1][<?php echo $val['sn']?>]" value="<?php echo $this->scores[$sn][1]?>" /></td>
<td class="score"><input type="text"  tabindex="<?php echo 200+$i++?>" size="3" name="score[2][<?php echo $val['sn']?>]" value="<?php echo $this->scores[$sn][2]?>" /></td>
<?php else:?>
<td class="score"><?php if(isset($this->scores[$sn][1]))echo $this->scores[$sn][1]?></td>
<td class="score"><?php if(isset($this->scores[$sn][2]))echo $this->scores[$sn][2]?></td>
<?php endif?>
<td class="score"><?php if(isset($this->scores[$sn][1]) and isset($this->scores[$sn][2])):?><?php echo round($this->scoreAverage[$sn],2)?><?php endif?></td>
<?php endif;?>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>

</form>
</div>
